/*************************************************************
 *Name:           Christopher Alexander
 *Email address:  alexanc2@onid.oregonstate.edu
 *Class name:     CS362
 *Assignment:     Assignment #3
 *Description:    This file (unittest2.c) contains unit tests
                  for isGameOver() in dominion.c
 *************************************************************/

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

#define NUM_UNIQUE_CARDS 27 //At least by my count this is right.


/*This function is used to generate a value for gameOver that
 *should be identical to the value calculated by isGameOver()*/
int preIsGameOver(struct gameState *preState) {

   int i;
   int numEmptyPiles;

   //if stack of Province cards is empty, the game ends
   if (preState->supplyCount[province] == 0){
      return 1;
   }

   //if three supply pile are at 0, the game ends
   numEmptyPiles = 0;
   for (i = 0; i < NUM_UNIQUE_CARDS; i++){
      if (preState->supplyCount[i] == 0){
         numEmptyPiles++;
      }
   }
   if ( numEmptyPiles >= 3){
      return 1;
   }

   return 0;
}

/*This function is used check the accuracy of isGameOver()*/
int checkIsGameOver(struct gameState *testState) {

   int testOver, preOver;
   struct gameState preState;

   memcpy(&preState, testState, sizeof(struct gameState));

   preOver = preIsGameOver(&preState);
   testOver = isGameOver(testState);

   if (preOver != testOver){
      printf("\tBUG FOUND RUNNING isGameOver()\n");
   }

   return 0;
}


int main() {
   int i, n;
   struct gameState G;

   printf("\nTesting isGameOver():\n");

   for (n = 0; n < 500; n++) {
      for (i = 0; i < sizeof(struct gameState); i++){
         ((char*)&G)[i] = (int) (Random() * 256);
      }

      for (i = 0; i < NUM_UNIQUE_CARDS; i++){
         /*Number 8 is used to try to make 3 cards have a count
         of 0 some of the time*/
         G.supplyCount[i] = (int) (Random() * 8);
      }

      //Half the time want to check the province card case.
      if ( ((int) (Random() * 2)) == 0){
         G.supplyCount[province] = 0;
      }

      checkIsGameOver(&G);
   }
    return 0;
}
